import { Component, OnInit, Input } from '@angular/core';
import { Router } from '@angular/router';
import { ModalController } from '@ionic/angular';
import { HttpService } from 'src/app/shared/service/http.service';
import { AppGlobalService } from '../../shared/service/app-global.service';
import { PageModalService } from '../../shared/service/page-modal-service';
import { MyrealEstateComponent } from '../myreal-estate/myreal-estate.component';
@Component({
  selector: 'app-open-member',
  templateUrl: './open-member.component.html',
  styleUrls: ['./open-member.component.scss'],
})
export class OpenMemberComponent implements OnInit {
  @Input() tagData: any; // 标签数据
  constructor(
    private appGlobalService: AppGlobalService,
    private pageModalService: PageModalService,
    private modalController: ModalController,
    public router: Router,
    private http: HttpService) { }

  ngOnInit() {
  }
  // 跳转开通
  go(obj) {

    if (obj.type === 'page') {
      const pageParams = {
        pageId: obj.target,
        params: obj.params,
        sessionId: localStorage.getItem('sessionId')
      };
      this.appGlobalService.goToForward(pageParams);
    } else if (obj.type === 'pageModal') {
      this.openRankingModal("myreal-estate", obj);
    } else if (obj.type === 'modal') { // 是否去发布dialog
      this.pageModalService.openPage(obj);
    }

  }

  showTips(msg) {
    this.appGlobalService.showToast(msg, 2000, 'middle');
  }

  async openRankingModal(className, item) {
    const params = {
      sessionId: localStorage.getItem("sessionId"),
      pageId: item.target,
      params: item.params
    };
    this.http.post('application/page', params, async res => {
      const tagData = JSON.parse(res.script).views[0].tags;
      // "ranking-modal"
      const modal = await this.modalController.create({
        showBackdrop: true,
        cssClass: className,
        component: MyrealEstateComponent,
        componentProps: { tagData }
      });
      await modal.present();
      // 监听销毁的事件
      const modalData = await modal.onDidDismiss(); // 获取关闭传回的值
      try {
        // 返回传回的值，重新调用接口
        console.log(modalData);
        if (modalData.data.itemdata) {
          const newObj = modalData.data.itemdata.link.params;
          Object.assign(newObj, { dateTime: new Date().getTime() });
          let param;
          if (modalData.data.itemdata.link.target) {
            param = {
              pageId: modalData.data.itemdata.link.target,
              params: newObj ? newObj : {},
              sessionId: localStorage.getItem('sessionId')
            };
          } else {
            localStorage.setItem('needRefreshTab1', 'true');
            param = {
              viewId: '',
              params: newObj ? newObj : {},
              sessionId: localStorage.getItem('sessionId')
            };
          }
          console.log(param);
          this.appGlobalService.goToForward(param);
        }
      } catch (error) {

      }

    });
  }
}
